package com.magicode.portal.dao.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.magicode.cms.entiy.base.Channel;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.NewsCollect;
import com.magicode.cms.entiy.base.Tag;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.entiy.base.UserSubscription;
import com.magicode.cms.entiy.base.Video;
import com.magicode.cms.entiy.base.VideoPlayHistory;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.util.ResultInfo;
import com.magicode.portal.dao.UserSubscriptionDao;

@SuppressWarnings("rawtypes")
@Repository("UserSubscriptionDao")
public class UserSubscriptionDaoImpl implements UserSubscriptionDao {

	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	/**
	 * 用户订阅标签
	 */
	public List<Map<String,String>> getSubscriptionTagByUser(String userid){
		List<Map<String,String>> objs = new ArrayList<Map<String,String>>();
		List<UserSubscription> list = commonDaoService.getListByHQL(" from "+UserSubscription.class.getName()+" t   left outer join fetch t.tag where t.user.id='"+userid+"' and t.tag is not null");
		for(UserSubscription usub : list){
			Map<String,String> map= new HashMap<String,String> ();
			Tag tag = usub.getTag();
			map.put("id", tag.getId());
			map.put("title", tag.getTitle());
			if(!objs.contains(map)){
				objs.add(map);
			}
		}
		return objs;
	}
	
	/**
	 * 用户订阅文章
	 */
	@Override
	public List<News> getSubscriptionNewsByUser(String userid){
		List<Map<String,String>> objs = new ArrayList<Map<String,String>>();
		String hql = "select distinct n from "+UserSubscription.class.getName()+" t , "+News.class.getName()+" n  left outer join fetch n.mainPic " +
				"  where t.tag in elements(n.tags) and  t.user.id =:id " ;
		Map<String,String> map = new HashMap<String, String>();
		map.put("id", userid);
		List<News> list = commonDaoService.getListBySQLMap(hql, map);
		return list;
	}
	/**
	 * 用户订阅频道
	 */
	public List<Map<String,String>> getSubscriptionChannelByUser(String userid){
		List<Map<String,String>> objs = new ArrayList<Map<String,String>>();
		List<UserSubscription> list = commonDaoService.getListByHQL(" from "+UserSubscription.class.getName()+" t  left outer join fetch t.channel  where t.user.id='"+userid+"' and t.channel is not null");
		for(UserSubscription usub : list){
			Map<String,String> map= new HashMap<String,String> ();
			Channel channel = usub.getChannel();
			map.put("id", channel.getId());
			map.put("title", channel.getTitle());
			objs.add(map);
		}
		return objs;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	//type 1.标签订阅
	public ResultInfo addUserSubscription(String userid, String refid, int type) {
		// TODO Auto-generated method stub
		if(null==userid||"".equals(userid)||null==refid||"".equals(refid)) return new ResultInfo(false,"userid["+userid+"]or refid["+refid+"] is null");
		User user = (User)commonDaoService.get(User.class,userid);
		UserSubscription us = new UserSubscription();
		us.setCreateTime(new Date());
		us.setUser(user);
		switch (type) {
		case 1:
			Tag tag =(Tag) commonDaoService.get(Tag.class, refid);
			us.setTag(tag);
			commonDaoService.save(us);
			break;
		case 2:
			Channel channel=(Channel)commonDaoService.get(Channel.class, refid);;
			us.setChannel(channel);
			commonDaoService.save(us);
			break;
			
		default:
			break;
		}
		return new ResultInfo(true,"");
	}

	public ICommonDaoService getCommonDaoService() {
		return commonDaoService;
	}

	public void setCommonDaoService(ICommonDaoService commonDaoService) {
		this.commonDaoService = commonDaoService;
	}
	@Override
	public boolean userIsSubscriptioned(String userid, String refid, int type) {
		
		if(null==userid ||"".equals(userid)||null==refid||"".equals(refid)||0==type){
			return false;
		}
		String hql = " from "+UserSubscription.class.getName()+" t  left outer join fetch t.user ";
		String where_hql=" where t.user.id='"+userid+"'  ";
		switch (type) {
		case 1:
			where_hql = where_hql+" and t.tag.id ='"+refid+"'";
			hql+=" left outer join fetch t.tag  ";
			break;
		case 2:
			hql+=" left outer join fetch t.channel  ";
			where_hql = where_hql+" and t.channel.id = '"+refid+"'";
			break;
		default:
			break;
		}
		List<UserSubscription> list = commonDaoService.getListByHQL(hql+where_hql);
		if(list.size()>0){
			return true;
		}
		return false;
	}
	@Override
	public Long getUserSubscriptionCount(String objid, int type) {
		String hql = "";
		switch (type) {
		//标签
		case 1:
			hql = "select t.id  from "+UserSubscription.class.getName()+" t " +
					" where  t.tag.id ='"+objid+"'";
			break;
		//栏目
		case 2:
			hql = "select t.id  from "+UserSubscription.class.getName()+" t " +
					" where  t.channel.id ='"+objid+"'";
			break;
		default:
			break;
		}
		if(!"".equals(hql)){
			Long count = Long.valueOf(commonDaoService.getListByHQL(hql).size());
			return count ;
		}
		return new Long(0);
	}
	@Override
	public Long getUserSubscriptions(String userid, int type) {
		String hql = "select count(t.id) from "+UserSubscription.class.getName()+" t  ";
		String where_hql=" where t.user.id='"+userid+"'  ";
		return commonDaoService.countByHqlMap(hql+where_hql, null);
	}
	
	/*************************************************************************************/
	public List<UserSubscription> getUserTagSubscription(String userid, String tagid) throws Exception{
		String hql =" from "+UserSubscription.class.getName()+" t where t.tag.id=:tagid  and t.user.id=:userid";
		Map<String,String> map = new HashMap<String,String>();
		map.put("tagid", tagid);
		map.put("userid", userid);
		List<UserSubscription> list = commonDaoService.queryList(hql, map);
		return list;
	}
	
	public List<UserSubscription> getUserChannelSubscription(String userid, String channelid) throws Exception{
		String hql =" from "+UserSubscription.class.getName()+" t where t.channel.id=:channelid  and t.user.id=:userid";
		Map<String,String> map = new HashMap<String,String>();
		map.put("channelid", channelid);
		map.put("userid", userid);
		List<UserSubscription> list = commonDaoService.queryList(hql, map);
		return list;
	}
	@Override
	public boolean deleteSubscriptionTagByUser(String userid, String tagid) {
		try {
			List<UserSubscription> list = this.getUserTagSubscription(userid, tagid);
			for (UserSubscription userSubscription : list) {
				commonDaoService.delete(userSubscription);
			}
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		
	}
	
	public boolean deleteSubscriptionChannelByUser(String userid, String channelid) {
		try {
			List<UserSubscription> list = this.getUserChannelSubscription(userid, channelid);
			for (UserSubscription userSubscription : list) {
				commonDaoService.delete(userSubscription);
			}
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}
	
	
}
